import { Nodes } from '@knno/dom';
import { form, Form as UIForm } from '../components/form/form';
import { formCell, FormCell as UIFormCell } from '../components/form/formCell';
import { JSXProps } from '@knno/dom/jsx';
import { AttrCheck, isBool, isNumber, isStr, jsxBuild } from './utils';

interface FormProps extends JSXProps<UIForm> {
	columns?: string;
}

const FORM_ATTRS: AttrCheck = {
	columns: isStr,
};

export function Form(props: FormProps, children: Nodes[]): UIForm {
	return jsxBuild(form(), props, children, FORM_ATTRS);
}

interface FormCellProps extends JSXProps<UIFormCell> {
	notice?: string;
	required?: boolean;
	caption?: string;
	help?: string;
	col?: number;
	row?: number;
	colspan?: string;
	rowspan?: string;
}

const CELL_ATTRS: AttrCheck = {
	help: isStr,
	caption: isStr,
	notice: isStr,
	required: isBool,
	col: isNumber,
	row: isNumber,
	colspan: isStr,
	rowspan: isStr,
};

export function FormCell(props: FormCellProps, children: Nodes[]): UIFormCell {
	return jsxBuild(formCell(), props, children, CELL_ATTRS);
}

export { UIForm, UIFormCell as UICell };
